/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VerSocios.java
 *
 * Created on Mar 12, 2011, 10:37:25 PM
 */

package forms;

import beans.Socio;
import dao.PersonaDao;
import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Guido Cresta
 */
public class VerSocios extends javax.swing.JFrame {

    /** Creates new form VerSocios */
    public VerSocios() {
        initComponents();
        cargarSocios();
        btn_editarSocio.setEnabled(false);
    }

    public void cargarSocios(){
        List<Socio> socios = PersonaDao.getSocios();
        DefaultTableModel table = (DefaultTableModel)this.table_socios.getModel();
        Vector row;
        for(Socio socio : socios){
            row = new Vector(2);
            
            row.add(0, socio.getLegajo());
            row.add(1, socio);
            
            table.addRow(row);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table_socios = new javax.swing.JTable();
        btn_editarSocio = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        table_socios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Legajo", "Nombre Completo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table_socios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                table_sociosMouseClicked(evt);
            }
        });
        table_socios.addHierarchyListener(new java.awt.event.HierarchyListener() {
            public void hierarchyChanged(java.awt.event.HierarchyEvent evt) {
                table_sociosHierarchyChanged(evt);
            }
        });
        jScrollPane1.setViewportView(table_socios);

        btn_editarSocio.setText("EDITAR");
        btn_editarSocio.setEnabled(false);
        btn_editarSocio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_editarSocioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)
                    .addComponent(btn_editarSocio, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
                .addComponent(btn_editarSocio)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void table_sociosHierarchyChanged(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_table_sociosHierarchyChanged
        
    }//GEN-LAST:event_table_sociosHierarchyChanged

    private void btn_editarSocioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_editarSocioActionPerformed
        int row = table_socios.getSelectedRow();
        if(row!=-1){
            DefaultTableModel model = (DefaultTableModel) table_socios.getModel();
            Vector vRow = (Vector)model.getDataVector().get(row);
            Socio socio = (Socio)vRow.get(1);
            CargarSocio cs = new CargarSocio(socio);
            cs.setIsEdit(true);
            cs.setVisible(true);
        }
    }//GEN-LAST:event_btn_editarSocioActionPerformed

    private void table_sociosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_table_sociosMouseClicked
        btn_editarSocio.setEnabled(true);
    }//GEN-LAST:event_table_sociosMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VerSocios().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_editarSocio;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table_socios;
    // End of variables declaration//GEN-END:variables

}
